<template>
	<view>
		
		
		
		<form>
			<view class="cu-form-group">
				<input placeholder="动态标题" name="input" v-model="title"></input>
			</view>
			<view style="width: 100%; height: 10rpx; background-color: #f1f1f1;"></view>
			<view class="aaa " >
				<textarea style="width: 100%;height: 300px;"  maxlength="-1" v-model="content" placeholder="动态内容"></textarea>
			</view>
			
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					图片上传
				</view>
				<view class="action">
					{{imgList.length}}/4
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			<view class="padding flex flex-direction">
						<button class="cu-btn bg-gradual-orange margin-tb-sm lg" @tap="sendData">发布</button>
					</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgList: [],
				baseurl:"",
				title:"",
				content:"",
				username:""
				
				
			}
		},
		onLoad() {
			this.baseurl=getApp().globalData.BASE_URL
			this.username=JSON.parse(uni.getStorageSync("USERS_KEY")).username
		},
		methods: {
			ChooseImage() {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '召唤师',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			}
			,
			// 发送表单数据到服务器
			async sendForm(){
				const innerThis=this
				uni.request({
					url:this.baseurl+"/state/UploadStateForm",
					data:{
						title:this.title,
						content:this.content,
						publisherName:this.username
					},
					success(res) {
						if(res.statusCode==200)
						{
								console.log("发送图片数据到服务器")
							const tempFilePaths = innerThis.imgList;
							
							for (let i = 0; i < tempFilePaths.length; i++) {
								uni.uploadFile({
									url: innerThis.baseurl+"/state/UploadImg",
									filePath: tempFilePaths[i],
									name: 'image',
									formData: {
										"stateTitle": innerThis.title
									},
									header: {
										"Content-Type": "multipart/form-data"
									},
									success: (res) => {
										console.log(res)
										if (res.statusCode === 200) {
											uni.showToast({
												title: "上传成功!"
											});
										} else {
											uni.showToast({
												title: "上传失败!",
												
											});
										}
									},
									fail(e) {
									}	
								})
							}
							uni.showToast({
								title: "上传成功!",
								success() {
									innerThis.toIndex()
								}
							});
							
						}
					},
					fail(res){
						console.log(res)
					}
				})
			}
			,
			// 发送图片数据到服务器
			toIndex(){
				uni.reLaunch({
					url:'index'
				})
				
			}
			,
			// 向服务器发送数据
			sendData(){
				this.sendForm();
			}
		}
	}
</script>

<style>
	.aaa{
	background-color: #FFFFFF;
	padding-left: 35rpx;
	padding-top: 35rpx;
	font-size: 40rpx;
	}

</style>
